package com.oocl.customer.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.customersystem.pojo.Order;
import com.oocl.customersystem.util.RequestUtil;

/**
 * Servlet implementation class UpdateOrders
 */
public class UpdateOrders extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateOrders() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String name =  request.getParameter("foodName");
		String type = request.getParameter("type");
		Map<String,Order> cart = (Map<String, Order>) request.getSession().getAttribute("cart");
		Order o = cart.get(name);
		if("1".equals(type)){
			o.setCount(o.getCount()-1);
		}else if("2".equals(type)){
			o.setCount(o.getCount()+1);
		}
		cart.put(name, o);
		request.getSession().setAttribute("cart", cart);
		List<Order> list =new  ArrayList<Order>();
		Set<String> set = cart.keySet();
		for(String fname:set){
			list.add(cart.get(fname));
		}
		String json = RequestUtil.wrapToJson(list);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
